<template>
  <div>
    <!-- 盘点 -->
    <el-dialog
      width="1100px"
      :title="`盘点批次:${title}`"
      destroy-on-close
      append-to-body
      :visible.sync="show"
      :before-close="cancel">
      <!-- 增删改查 -->
      <el-row style="margin: 15px 0" :gutter="10">
        <!-- v-hasPermi="['distribution:purchase:createPurchaseOrder']" -->
        <el-col :span="1.5">
          <el-button
            type="success"
            icon="el-icon-plus"
            plain
            size="mini"
            v-hasPermi="['distribution:stockCheck:appendPositionNo']"
            @click="handleAppend">
            <!-- 追加库位 -->
            追加库位
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            icon="el-icon-delete"
            plain
            v-hasPermi="['distribution:stockCheck:clearStockCheck']"
            size="mini"
            @click="handleDelete()">
            <!-- 清空盘点 -->
            清空盘点
          </el-button>
        </el-col>
      </el-row>
      <el-table ref="TableRef" :data="tableData">
        <el-table-column show-overflow-tooltip prop="skuNo" :label="$t('sku')" align="center" width="150" />
        <!-- 库位号 -->
        <el-table-column
          show-overflow-tooltip
          prop="positionNo"
          :label="$t('库位号')"
          align="center"
          width="110" />
        <!-- 盘点类型 -->
        <el-table-column show-overflow-tooltip prop="type" label="盘点类型" align="center">
          <!-- 
            盘点类型：
            1.扫码盘点
            2.手动+扫码盘点
        -->
          <template slot-scope="{ row }">
            <span v-if="row.type == 1">扫码盘点</span>
            <span v-if="row.type == 2">手动+扫码盘点</span>
          </template>
        </el-table-column>
        <el-table-column show-overflow-tooltip prop="type" label="图片" align="center">
          <template slot-scope="{ row }">
            <el-image
              style="width: 40px; height: 40px; object-fit: cover"
              :src="row.imgUrl + '?x-oss-process=image/resize,w_100/quality,q_70'"
              :preview-src-list="[row.imgUrl]" />
          </template>
        </el-table-column>
        <!-- 快照数量 -->
        <el-table-column show-overflow-tooltip prop="stockSnap" label="快照数量" align="center" />
        <!-- 确认数量 -->
        <el-table-column show-overflow-tooltip prop="confirmNum" label="确认数量" align="center" />
        <!-- 差异数量 -->
        <el-table-column show-overflow-tooltip prop="differenceNum" label="差异数量" align="center">
          <template slot-scope="{ row }">
            <div
              :style="{
                color: row.differenceNum >= 0 ? '' : 'red',
              }">
              {{ row.differenceNum }}
            </div>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column align="center" fixed="right" :label="$t('操作')" :width="calWidth(300, 350, 300)">
          <!--  根据类型显示操作
            扫码盘点
            手动+扫码盘点
            删除 -->
          <!-- 1.扫码盘点
            2.手动+扫码盘点 -->

          <template slot-scope="{ row }">
            <el-button
              v-if="row.type == '1'"
              style="margin: 0 15px"
              type="text"
              size="small"
              @click="handleScan(row, '1')">
              <!-- 扫码盘点 -->
              扫码盘点
            </el-button>
            <el-button
              v-if="row.type == '2'"
              style="margin: 0 15px"
              type="text"
              size="small"
              @click="handleScan(row, '2')">
              <!-- 手动+扫码盘点 -->
              手动+扫码盘点
            </el-button>
            <el-button
              @click="handleDelete(row.id)"
              type="text"
              size="small"
              v-hasPermi="['distribution:stockCheck:clearStockCheck']">
              删除盘点
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="pageTotal > 0"
        :total="pageTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList" />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">
          <!-- 关闭 -->
          {{ $t('关闭') }}
        </el-button>
      </div>
    </el-dialog>
    <!-- 扫码盘点 -->
    <Scan
      @update="getList"
      :ScanType="scan.ScanType"
      :show.sync="scan.show"
      :checkPositionId="scan.checkPositionId"
      :positionNo="scan.positionNo"
      :stockSnap="scan.stockSnap" />

    <!-- 追加 -->
    <Append :show.sync="append.show" @close="getList" :checkId="append.checkId" />
  </div>
</template>

<script>
import Scan from './components/scan.vue'
import HandScan from './components/scan.vue'
import Append from './components/append.vue'
import { getStockCheckDetailsXml, clearStockCheck } from '@/api/distribution/sku/Inventory'

export default {
  activated() {
    this.$nextTick(() => this.$refs['TableRef']?.doLayout())
  },
  components: {
    Scan,
    HandScan,
    Append,
  },
  props: {
    /**
     * title
     */
    title: {
      type: String,
      default: '',
    },
    /**
     * id
     */
    id: {
      type: [String, Number],
      required: true,
    },
    /**
     * 控制显示隐藏
     */
    show: {
      type: Boolean,
      required: true,
      default: false,
    },
  },
  data() {
    return {
      /**扫码盘点 */
      scan: {
        show: false,
        checkPositionId: '',
        positionNo: '',
        stockSnap: 0,
        ScanType: '',
      },
      /**手动+扫码盘点 */
      handScan: {
        show: false,
      },
      /**追加*/
      append: {
        show: false,
        checkId: '',
      },
      pageTotal: 0,
      tableData: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        checkId: '',
      },
    }
  },
  watch: {
    show: function (newValue) {
      if (newValue) {
        this.queryParams.pageNum = 1
        this.queryParams.pageSize = 10
        this.queryParams.checkId = this.id
        this.getList()
      }
    },
  },
  methods: {
    handleDelete(positionId) {
      this.$confirm(this.$t(positionId ? '确认删除?' : '确认清空?'), this.$t('提示'), {
        type: 'warning',
      }).then(() => {
        clearStockCheck(this.queryParams.checkId, positionId).then((res) => {
          const { code, data, msg } = res
          if (code === 200) {
            this.$message({ type: 'success', message: msg })
            this.getList()
          }
        })
      })
    },
    handleAppend() {
      this.append.show = true
      this.append.checkId = this.queryParams.checkId
    },
    handleScan(row, type) {
      this.scan.ScanType = type
      this.scan.checkPositionId = row.id
      this.scan.positionNo = row.positionNo
      this.scan.stockSnap = row.stockSnap
      this.scan.show = true
    },
    handleHandScan() {
      this.handScan.show = true
    },
    getList() {
      this.$emit('getList')
      getStockCheckDetailsXml(this.queryParams).then((res) => {
        const { code, rows, total } = res
        if (code === 200) {
          this.tableData = rows
          this.pageTotal = total
        }
      })
    },
    cancel() {
      this.$emit('update:show', false)
    },
    handleExport() {
      this.download(
        '',
        {
          ...this.queryParams,
        },
        `export_${new Date().getTime()}.xlsx`,
      )
    },
  },
}
</script>
<style scoped lang="scss"></style>
